
import tkinter as tk

class MyWindow:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title('我的窗口')
        self.window.geometry('500x300') # 窗体的大小

        # 标签
        self.var = tk.StringVar()
        self.label = tk.Label(self.window, textvariable=self.var, bg='green', font=('Arial', 12), width=15, height=2)
        self.label.pack()
        self.on_hit = False

        # 按钮
        self.button = tk.Button(self.window, text='quit', width=15, height=2, command=self.window.quit)
        self.button.pack()

    def hit_me(self, event):
        global on_hit
        if self.on_hit == False:
            self.on_hit = True
            self.var.set('you hit me')
        else:
            self.on_hit = False

    def change_red(self, event):
        self.label.config(bg='red')

    def change_blue(self, event):
        self.label.config(bg='blue')

    def clear_text(self, event):
        self.var.set('')
        self.on_hit = False

    def bind(self):
        self.label.bind('<Button-1>', self.hit_me) # 左键点击
        self.label.bind('<Button-2>', self.hit_me) # 中键点击
        self.label.bind('<Button-3>', self.hit_me) # 右键点击
        self.label.bind('<Double-Button-1>', self.clear_text) # 左键双击
        self.label.bind('<Double-Button-2>', self.hit_me) # 中键双击
        self.label.bind('<Double-Button-3>', self.hit_me) # 右键双击
        self.label.bind('<Triple-Button-1>', self.hit_me) # 左键三击
        self.label.bind('<Triple-Button-2>', self.hit_me) # 中键三击
        self.label.bind('<Triple-Button-3>', self.hit_me) # 右键三击
        self.label.bind('<Enter>', self.change_red) # 鼠标进入
        self.label.bind('<Leave>', self.change_blue) # 鼠标离开

    def run(self):
        self.window.mainloop()


if __name__ == '__main__':
    window = MyWindow()
    window.bind()
    window.run()